/*
This -do file generates tables of regression analysis, graphs to be used in the 
analysis of the Malawi student incentive project
 
Last version: Hyuk Son, 10/04/2016
Non-standard Stata:
                estout.ado
				coefplot.ado
 
uses:
	merged_y1y2.dta
		
Contents
 
*             0 Set Up
*                   0.1 Assign groups of control variables
*					0.2 Sample restriction
*
*             3 Graphs	
*					3.1 Understanding of the program and Expectation of the scholarship
*					3.2 Exam scores at follow-up by Baseline overall rank
*					3.3 Coefficient of scholarship program effect
*					3.4 Feedback effect on follow-up exam score by Mid-term overall rank
*					3.5 Understanding of the program and Expectation of the scholarship (by bin rank(%)
*					3.6 Exam scores at follow-up by Baseline overall rank -long

Note: Figure 1, 2, A1, A2, B1 are not empirical
*/

set more off
clear all
********************************************************************************************************************
/*
  					0	Set up
*/
********************************************************************************************************************
set more off
clear all

include "$do_path/programs.do"

****************************************************
/* 		0.2 Read in data		 */
****************************************************
use "$data_path/merged_y1y2.dta", clear

*** variable label 
	lab var male "Male"
	lab var merit "\textit{Standard}"
	lab var relative "\textit{Relative}"
	lab var b1_top15 "B/L Top 15\%"
	lab var m_b1top15 "\textit{Std.} x Top 15\%"
	lab var r_b1top15 "\textit{Rel.} x Top 15\%"	
	lab var b1_sub15 "B/L Subg. Top 15\%"
	lab var m_b1sub15 "\textit{Std.} x Subg. Top 15\%"
	lab var r_b1sub15 "\textit{Rel.} x Subg. Top 15\%"	
	lab var b1_studyhour "Study hours per week"
	lab var b1_pct "Baseline overall rank(%)"
	lab var b1_hhsize "Household size"
	lab var meritfeed "\textit{Standard} x FB"
	lab var relativefeed "\textit{Relative} x FB"


****************************************************
/* 		0.3 Assign groups of outcome and control variables		 */
****************************************************
 * 0.1.1 Assign outcome variables
	local t2a age male b1_q105_chewa b1_hhsize b1_s5_asset
	local t3 understand_scholarship f1_understand_scholarship expect_scholarship2 f1_expect_scholarship2  
	local t4 f1_pct f1_z_total f1_z_math f1_s9_score  
	local t5 atd_abs_aa0m f1_studyhour_ac y1_jun15_abs y1_may15_abs y1_apr15_abs y1_mar15_abs
	local t6 f1_motivation f1_s7_selfesteem f1_s7_conscientious f1_s7_grit
	local t7 f1_teacherindex f1_parentaleffort f1_q503 f1_parentalinvolve

	local demo2 male b1_q105_chewa b1_hhsize b1_s5_asset	

	global cons `cons'

 * 0.1.3 Generate variables for graphs
 	local outcome f1_z_total stu_tot_z_16_1 stu_tot_z_16_2 atd_abs_aa0m f1_studyhour_ac s1_studyhour_ac f1_motivation s1_motivation f1_s7_selfesteem s1_selfesteem f1_s7_conscientious s1_conscientious


****************************************************
/* 		0.4 conditions for graphs		 */
****************************************************
	loc scholar_graph cat1(merit) cat2(relative) cat3(control) cl1(blue) cl2(red) cl3(green) lp1(solid) lp2(longdash) lp3(shortdash)  label1(Standard) label2(Relative) label3(Control)
	loc feedback_graph cat1(feedback) cat2(nofeed) cl1(black) cl2(black) lp1(solid) lp2(longdash) label1(Feedback) label2(No Feedback)
	loc feedback_graph1 cat1(meritfeed) cat2(meritnofeed) cl1(blue) cl2(blue) lp1(solid) lp2(longdash) label1(Feedback) label2(No Feedback)
	loc feedback_graph2 cat1(relativefeed) cat2(relativenofeed) cl1(red) cl2(red) lp1(solid) lp2(longdash) label1(Feedback) label2(No Feedback)
	loc feedback_graph3 cat1(controlfeed) cat2(controlnofeed) cl1(green) cl2(green) lp1(solid) lp2(longdash) label1(Feedback) label2(No Feedback)

/*------------------------------------------------ 		  

		3 Main Results

------------------------------------------------*/
*** graph style macro
loc graphstyle 	xline(85, lc(black)) ytitle("") ///
	legend(label(1 "Standard") label(2 "Relative") label(3 "Control") rows(1)) ///
	plotregion(style(none)) graphregion(color(white)) 

/*------------------------------------------------ 		  
 3.1 Understanding of the program and Expectation of the scholarship 
------------------------------------------------*/
preserve
keep if y1_sub==1

loc outcome understand_scholarship f1_understand_scholarship expect_scholarship2 f1_expect_scholarship2 

*** by overall rank
forval i = 1/4 {
	loc v: word `i' of `outcome'
	loc t: word `i' of "After Announcement" "1st follow-up" "After Announcement" "1st follow-up"

		twoway lpoly `v' b1_pct if y1_rand==1, lc(blue) lpattern(solid) ///
		|| lpoly `v' b1_pct if y1_rand==2, lc(red) lpattern(longdash) ///
		|| lpoly `v' b1_pct if y1_rand==3, lc(green) lpattern(shortdash) ///
		`graphstyle' ylabel(0(0.2)1) ///
		xtitle("Baseline overall rank(%)") title("`t'") ///

		graph save "$figure_path/`v'.gph", replace
}

 ** Understanding
	graph combine "$figure_path/understand_scholarship.gph" "$figure_path/f1_understand_scholarship.gph", ///
				ycommon plotregion(style(none)) graphregion(color(white)) iscale(1) ///
				xsize(9)
	
	graph export "$figure_path/understand_pct_58.png", replace

 ** expectation
	graph combine "$figure_path/expect_scholarship2.gph" "$figure_path/f1_expect_scholarship2.gph", ///
				ycommon plotregion(style(none)) graphregion(color(white)) iscale(1) ///
				xsize(9)
	
	graph export "$figure_path/expect_pct_58.png", replace


	
*** by bin rank
forval i = 1/4 {
	loc v: word `i' of `outcome'
	loc t: word `i' of "After Announcement" "1st follow-up" "After Announcement" "1st follow-up"

		twoway lpoly `v' b1_subpct if y1_rand==1, lc(blue) lpattern(solid) ///
		|| lpoly `v' b1_subpct if y1_rand==2, lc(red) lpattern(longdash) ///
		|| lpoly `v' b1_subpct if y1_rand==3, lc(green) lpattern(shortdash) ///
		`graphstyle' ylabel(0(0.2)1)  ///
		xtitle("Baseline bin rank(%)") title("`t'") ///

		graph save "$figure_path/`v'.gph", replace
}

 ** Understanding
	graph combine "$figure_path/understand_scholarship.gph" "$figure_path/f1_understand_scholarship.gph", ///
				ycommon plotregion(style(none)) graphregion(color(white)) iscale(1) ///
				xsize(9)
	
	graph export "$figure_path/understand_sub_58.png", replace

 ** expectation
	graph combine "$figure_path/expect_scholarship2.gph" "$figure_path/f1_expect_scholarship2.gph", ///
				ycommon plotregion(style(none)) graphregion(color(white)) iscale(1) ///
				xsize(9)
	
	graph export "$figure_path/expect_sub_58.png", replace


*** erase graphs
foreach v of varlist `outcome' {
	erase "$figure_path/`v'.gph" 
}

restore

/*------------------------------------------------ 	
 3.2 Exam scores at follow-up by Baseline overall rank 	
------------------------------------------------*/
preserve
keep if y1_sub==1

loc outcome f1_z_total

*** by overall rank
forval i = 1/1 {
	loc v: word `i' of `outcome'
	loc t: word `i' of "Final Exam" 

		twoway lpoly `v' b1_pct if y1_rand==1, lc(blue) lpattern(solid) ///
		|| lpoly `v' b1_pct if y1_rand==2, lc(red) lpattern(longdash) ///
		|| lpoly `v' b1_pct if y1_rand==3, lc(green) lpattern(shortdash) ///
		`graphstyle' ylabel("-1.5(0.5)1") ///
		xtitle("Baseline overall rank(%)") title("`t'") ///

		graph export "$figure_path/s_exam_58.png", replace
}

	
*** by bin rank
forval i = 1/1 {
	loc v: word `i' of `outcome'
	loc t: word `i' of "Final Exam"

		twoway lpoly `v' b1_subpct if y1_rand==1, lc(blue) lpattern(solid) ///
		|| lpoly `v' b1_subpct if y1_rand==2, lc(red) lpattern(longdash) ///
		|| lpoly `v' b1_subpct if y1_rand==3, lc(green) lpattern(shortdash) ///
		`graphstyle' ylabel("-1.5(0.5)1") ///
		xtitle("Baseline bin rank(%)") title("`t'") ///

		graph export "$figure_path/s_exam_sub_58.png", replace
}

restore



/*------------------------------------------------ 		  

		4 Appendix

------------------------------------------------*/
*** graph style macro
loc graphstyle 	xline(85, lc(black)) ytitle("") ///
	legend(label(1 "Feedback") label(2 "No Feedback") rows(1)) ///
	plotregion(style(none)) graphregion(color(white)) 

/*------------------------------------------------ 		  
 3.4 Feedback effect on follow-up exam score by Mid-term overall rank 	
------------------------------------------------*/
preserve
keep if y1_sub==1

loc outcome f1_z_total

*** by overall rank
forval i = 1/1 {
	loc v: word `i' of `outcome'
	loc t: word `i' of "Final Exam" 

		twoway lpoly `v' m1_pct if feedback==1, lc(black) lpattern(solid) ///
		|| lpoly `v' m1_pct if feedback==0, lc(black) lpattern(longdash) ///
 		`graphstyle' ///
		xtitle("Mid-term overall rank(%)") title("`t'") ///

		graph export "$figure_path/f_exam.png", replace
}


forval i = 1/1 {
	loc v: word `i' of `outcome'
	loc t: word `i' of "Final Exam" 

	forval j = 1/3 {
		loc c: word `j' of blue red green 
		loc t: word `j' of Standard Relative Control
	
			twoway lpoly `v' m1_pct if y1_rand==`i'&feedback==1, lc(`c') lpattern(solid) ///
			|| lpoly `v' m1_pct if y1_rand==`i'&feedback==0, lc(`c') lpattern(longdash) ///
			`graphstyle' ///
			xtitle("Mid-term overall rank(%)") title("`t'") 

			graph save "$figure_path/`t'.gph", replace
	}
}

 ** Understanding
	graph combine "$figure_path/standard.gph" ///
				"$figure_path/relative.gph" ///
				"$figure_path/control.gph", ///
				r(1) ycommon plotregion(style(none)) graphregion(color(white)) iscale(1) ///
				xsize(11)
	
	graph export "$figure_path/f_exam_pct_58.png", replace



*** erase graphs
	erase "$figure_path/standard.gph" 
	erase "$figure_path/relative.gph" 
	erase "$figure_path/control.gph" 


